package com.ellricky.baselib.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.RequiresApi
import androidx.core.content.ContextCompat
import com.ellricky.baselib.R
import kotlinx.android.synthetic.main.base_layout_achievement.view.*

class AchievementLayout : FrameLayout {

    private var logo1: Drawable
    private var logo2: Drawable
    private var logo3: Drawable

    private var titleOne: String = ""
    private var titleTwo: String = ""
    private var titleThree: String = ""

    private var descOne: String = ""
    private var descTwo: String = ""
    private var descThree: String = ""

    private var isReachOne: Boolean = true
    private var isReachTwo: Boolean = true
    private var isReachThree: Boolean = true

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)


    constructor(context: Context, attributeSet: AttributeSet?, def: Int) : super(context, attributeSet, def) {
        View.inflate(context, R.layout.base_layout_achievement, this)

        val typeArr = context.obtainStyledAttributes(attributeSet, R.styleable.AchievementLayout)
        titleOne = typeArr.getString(R.styleable.AchievementLayout_achieve_text_one) ?: ""
        titleTwo = typeArr.getString(R.styleable.AchievementLayout_achieve_text_two) ?: ""
        titleThree = typeArr.getString(R.styleable.AchievementLayout_achieve_text_three) ?: ""

        descOne = typeArr.getString(R.styleable.AchievementLayout_achieve_desc_one) ?: ""
        descTwo = typeArr.getString(R.styleable.AchievementLayout_achieve_desc_two) ?: ""
        descThree = typeArr.getString(R.styleable.AchievementLayout_achieve_desc_three) ?: ""

        logo1 = typeArr.getDrawable(R.styleable.AchievementLayout_achieve_logo_one)
                ?: ContextCompat.getDrawable(context, R.drawable.base_icon_achieve_1)!!
        logo2 = typeArr.getDrawable(R.styleable.AchievementLayout_achieve_logo_two)
                ?: ContextCompat.getDrawable(context, R.drawable.base_icon_achieve_1)!!
        logo3 = typeArr.getDrawable(R.styleable.AchievementLayout_achieve_logo_three)
                ?: ContextCompat.getDrawable(context, R.drawable.base_icon_achieve_1)!!

        isReachOne = typeArr.getBoolean(R.styleable.AchievementLayout_achieve_is_reach_one, true)
        isReachTwo = typeArr.getBoolean(R.styleable.AchievementLayout_achieve_is_reach_two, true)
        isReachThree = typeArr.getBoolean(R.styleable.AchievementLayout_achieve_is_reach_three, true)

        tv_title_1.text = titleOne
        tv_title_2.text = titleTwo
        tv_title_3.text = titleThree

        tv_desc_1.text = descOne
        tv_desc_2.text = descTwo
        tv_desc_3.text = descThree

        iv_logo_1.setImageDrawable(logo1)
        iv_logo_2.setImageDrawable(logo2)
        iv_logo_3.setImageDrawable(logo3)

        if (!isReachOne) {
            tv_title_1.alpha = 0.3f
            tv_desc_1.alpha = 0.3f
            iv_logo_1.alpha = 0.3f
        }

        if (!isReachTwo) {
            tv_title_2.alpha = 0.3f
            tv_desc_2.alpha = 0.3f
            iv_logo_2.alpha = 0.3f
        }

        if (!isReachThree) {
            tv_title_3.alpha = 0.3f
            tv_desc_3.alpha = 0.3f
            iv_logo_3.alpha = 0.3f
        }
        typeArr.recycle()
    }
}